package com.wxd.util;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.wxd.action.LoginAction;
import com.wxd.bean.UserInfo;

public class CheckLoginInterceptor extends AbstractInterceptor {
	public static final String LOGIN_KEY = "userInfo";
	public static final String LOGIN_PAGE = "login";

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Object action = invocation.getAction();

		if (action instanceof LoginAction) {
			return invocation.invoke();
		}
		Map<String, Object> session = invocation.getInvocationContext()
				.getSession();

		Object object = session.get(LOGIN_KEY);
		if (object == null) {
			return LOGIN_PAGE;
		} else if (object instanceof UserInfo) {
			return invocation.invoke();
		} else {
			return LOGIN_PAGE;
		}
	}
}
